-- UIReagentDetail
-- Create by chendh Jul/04/2015
-- 试剂详细信息界面

require "game/ui/base/TextColorM"

-- UIReagentDetail继承自Layer
UIReagentDetail = class("UIReagentDetail", function()
    return cc.Layer:create();
end);

function UIReagentDetail.create(goodsIndex)
    return UIReagentDetail.new(goodsIndex);
end

-- 构造函数
function UIReagentDetail:ctor(goodsIndex)
    self:setName("UIReagentDetail");

    -- 初始化
    local node = cc.CSLoader:createNode("layout/shop/BlackMarketGoodsDetail.csb");
    self:addChild(node);
    self.node = node;

    self:setLocalZOrder(UIMgr.TOP_MENU_ZORDER + 1);

    -- 记录商品信息
    self.goodsIndex = goodsIndex;
    local goodsInfo = ReagentM.getReagentInfo(self.goodsIndex);
    self.goods = goodsInfo.goods;
    self.price = goodsInfo.price;

    -- 注册事件处理回调函数
    self:registerEventCallback();

    -- 注册点击事件
    self:registerTouchEvent();

    -- 重绘
    self:redraw();

    -- 适配
    self:resize();
end

-- 注册事件处理回调函数
function UIReagentDetail:registerEventCallback()

end

-- 注册点击事件
function UIReagentDetail:registerTouchEvent()
    -- 注册返回按钮点击事件
    local btnBack = findChildByName(self.node, "BT/btn_back");
    local function onBackClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            -- 关闭当前界面
            AudioM.playFx("button_return");
            UIMgr.getCurrentScene():removeFormByName("UIReagentDetail");
        end
    end
    AddBackEventHandler(self, btnBack, onBackClick);
    TextStyleM.setTextStyle(btnBack, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnBack:setTitleText(getLocStr("btn_text_back"));

    -- 注册确认购买按钮点击事件
    local btnBuy = findChildByName(self.node, "CT/btn_buy");
    local function onBuyClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            AudioM.playFx("button_click");
            -- 尝试购买
            self:tryBuy();
        end
    end
    btnBuy:addTouchEventListener(onBuyClick);
    TextStyleM.setTextStyle(btnBuy, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnBuy:setTitleText(getLocStr("confirm_to_modulation"));
end

-- 重绘
function UIReagentDetail:redraw()
    -- 显示商品名称
    local CT = findChildByName(self.node, "CT");
    local nameLabel = CT:getChildByName("name");
    local goods = self.goods;
    local classId = goods[2];
    local goodNum = goods[3];
    local itemInfo = ItemM.query(classId);

    local nameDesc;
    if goodNum > 1 then
        nameDesc = string.format(getLocStr("item_bonus_format"), itemInfo["name"], goodNum);
    else
        nameDesc = itemInfo["name"];
    end
    TextStyleM.setTextStyle(nameLabel, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_DARK_RED);
    nameLabel:setString(nameDesc);

    -- 显示商品描述
    local desc = ItemM.getItemDesc(classId);
    local detail = ItemM.getItemDetail(classId);

    local descLabel = CT:getChildByName("desc");
    TextStyleM.setTextStyle(descLabel, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_DARK_BROWN);
    descLabel = applyString(descLabel, desc);

    local detailLabel = CT:getChildByName("detail");
    TextStyleM.setTextStyle(detailLabel, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_DARK_BROWN);
    detail = "[blue]" .. detail .. "[-]";
    detailLabel = applyString(detailLabel, detail);

    -- 显示商品价格
    local price = self.price;
    local payAttrib   = price[2];
    local priceAmount = price[3];

    -- 降低购买试剂金币消耗的属性
    if price[2] == "money" then
        local prop = PropM.combine(ME.user, "reagent_cost", 1);
        priceAmount = PropM.apply(prop, priceAmount);
    end
    local ownAmount = ME.user:queryAttrib(payAttrib);
    local attribDesc = string.format(getLocStr("attrib_value_format"), ownAmount, priceAmount);
    if priceAmount > ownAmount then
        attribDesc = "[red]" .. attribDesc .. "[-]";
    end

    local fieldName = FieldsM.getFieldName(payAttrib);
    local costDesc = string.format(getLocStr("modulation_cost_attrib2"), fieldName, attribDesc);

    costDesc = costDesc .. "\n";

    -- 显示拥有商品数量
    local amountLabel = CT:getChildByName("own_amount");
    TextStyleM.setTextStyle(amountLabel, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_GREEN);
    local amount = ItemM.getOwnedAmount(ME.user, classId);
    costDesc = costDesc .. string.format(getLocStr("own_goods_amount2"), amount);
    amountLabel = applyString(amountLabel, costDesc);

    -- 自适应所有组件
    local bg = findChildByName(self.node, "CT/bg");
    local btnBuy = findChildByName(self.node, "CT/btn_buy");
    AlignM.adaptLayout(bg, AlignM.BG_SIZE_NORMAL,
        { nameLabel, descLabel, detailLabel, amountLabel, btnBuy});

    -- 显示商品阶位
    local rank = ItemM.query(classId, "rank") or 0;
    if rank > 0 then
        local starNode = findChildByName(self.node, "CT/star_node");
        local layer = createStarLayer(rank, "images/ui/symbol/star_large.png", "right");
        if layer then
            starNode:removeAllChildren();
            starNode:addChild(layer);
        end
        starNode:setPositionY(nameLabel:getPositionY());
    end
end

-- 适配
function UIReagentDetail:resize()
    local node = self.node;

    -- bg居中显示并充满整个屏幕
    AlignM.fitToScreen(node, "bg");

    -- CT居中并缩放至合适大小
    AlignM.alignToCenter(node, "CT");

    -- BT置于底部并缩放至合适大小
    AlignM.alignToBottomCenter(node, "BT");
end

-- 尝试购买
function UIReagentDetail:tryBuy()
    -- 判断能否购买
    local ret = ReagentM.canBuyReagent(self.goodsIndex);
    if ret ~= true then
        AudioM.playFx("market_buy_failed");
        if string.endWith(ret, "_lack") then
            -- 属性不足
            local field = string.sub(ret, 1, -6);
            if field == "gem" then
                showGemLackConfirm();
            else
                local fieldName = FieldsM.getFieldName(field);
                local msg = string.format(getLocStr("fail_to_do_operate"), fieldName, getLocStr("buy"));
                alert(msg);
            end
        elseif ret == "not active" then
            alert(getLocStr("reagent_no_active"));
        else
            -- 未知原因
            cclog("购买商品失败，原因：" .. ret );
        end
        -- 关闭当前界面
        UIMgr.getCurrentScene():removeFormByName("UIReagentDetail");
        return;
    end

    -- 执行购买
    AudioM.playFx("market_buy_success");
    Operation.cmd_buy_reagent(self.goodsIndex);

    -- 关闭当前界面
    UIMgr.getCurrentScene():removeFormByName("UIReagentDetail");
end

